package com.heima.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @author stone
 * @description 针对表【ad_channel(频道信息表)】的数据库操作Service实现
 * @createDate 2023-02-21 11:48:45
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel>
        implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {

        if (Objects.isNull(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 检查分页参数
        dto.checkParam();

        Page<AdChannel> channelPage = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery()
                .like(StringUtils.isNotEmpty(dto.getName()), AdChannel::getName, dto.getName())
                .eq(!Objects.isNull(dto.getStatus()), AdChannel::getStatus, dto.getStatus())
                .orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> pageResult = this.page(channelPage, wrapper);

        return new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    @Transactional
    public ResponseResult insert(AdChannel channel) {
        if (StringUtils.isEmpty(channel.getName()) || channel.getName().length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空，不能大于10个字符");
        }

        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称已存在");
        }
        channel.setCreatedTime(new Date());
        this.save(channel);

        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult update(AdChannel adChannel) {

        if (StringUtils.isEmpty(adChannel.getName()) || adChannel.getName().length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空，不能大于10个字符");
        }

        if (Objects.isNull(adChannel.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不能为空");
        }

        // 查询频道信息
        AdChannel oldChannel = this.getById(adChannel.getId());
        // 频道name不为空，修改频道 判断参数频道与查询频道信息 是否一致
        if (StringUtils.isNotEmpty(oldChannel.getName()) && !oldChannel.getName().equals(adChannel.getName())) {
            // 不一致，判断参数频道是否已存在
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "修改失败，频道已存在");
            }
        }

        this.updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult deleteById(Integer id) {

        if (Objects.isNull(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不能为空");
        }
        AdChannel channel = this.getById(id);
        if (Objects.isNull(channel)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道不存在");
        }
        // 删除关闭状态的频道
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW, "频道为开启状态，不能删除");
        }

        this.removeById(id);

        return ResponseResult.okResult();
    }


}




